# 输入：文件路径
# 输出：vtkActor
import vtkmodules.all as vtk


class IPlyReader():
    v_reader = vtk.vtkPLYReader()
    v_actor = vtk.vtkActor()
    v_mapper = vtk.vtkPolyDataMapper()
    v_gaussian_mapper = vtk.vtkPointGaussianMapper()

    m_use_point_mapper = False

    def __init__(self, options=None):
        self._initPolyMapper()
        self._initGaussianMapper()

        if (options is not None):
            self.m_use_point_mapper = options.usePointMapper

        if (self.m_use_point_mapper):
            self.v_actor.SetMapper(self.v_gaussian_mapper)
        else:
            self.v_actor.SetMapper(self.v_mapper)

    def _initPolyMapper(self):
        self.v_mapper.SetInputConnection(self.v_reader.GetOutputPort())
        self.v_mapper.SetScalarVisibility(0)  # 关闭颜色映射

    def _initGaussianMapper(self):
        self.v_gaussian_mapper.SetInputConnection(self.v_reader.GetOutputPort())
        self.v_gaussian_mapper.SetScaleFactor(0.01)
        self.v_gaussian_mapper.EmissiveOff()

    def SetFileName(self, file_path, force_update=False):
        self.v_reader.SetFileName(file_path)
        if (force_update):
            self.Update()

    def Update(self):
        self.v_reader.Update()

    def GetActor(self):
        return self.v_actor
